package teamproject.graphEditor.graphPainters;

import java.awt.Point;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Random;

import teamproject.graphEditor.Controller;
import teamproject.graphEditor.Node;
import teamproject.graphEditor.PDEEdge;
import edu.uci.ics.jung.graph.DirectedGraph;
import edu.uci.ics.jung.visualization.VisualizationViewer;

public class RandomGraphPainter implements GraphPainter {

	private Controller control;
	private long seed = System.currentTimeMillis();

	public RandomGraphPainter(Controller control) {
		this.control = control;
	}

	@Override
	public void paintGraph(DirectedGraph<Node, PDEEdge> g) {

		// control.getGraphEditor().clearGraph();
		// DirectedGraph<Node, PDEEdge> graph =
		// control.getGraphEditor().getGraph();
		VisualizationViewer<Node, PDEEdge> vv = control.getGraphEditor()
				.getVisualizationViewer();
		vv.getGraphLayout().setGraph(g);
		// Collection<Node> nodes = g.getVertices();
		// Collection<PDEEdge> edges = g.getEdges();
		// control.getNodeFactory().setNodes(g.getVertices());
		/*
		 * for (Node node : nodes) { graph.addVertex(node); } for (PDEEdge edge
		 * : edges) { Pair<Node> endpoints = g.getEndpoints(edge);
		 * graph.addEdge(edge, endpoints.getFirst(), endpoints.getSecond()); }
		 */

		Random random = new Random(seed);
		int maxX = vv.getSize().width - 10;
		int maxY = vv.getSize().height - 10;
		int x;
		int y;
		Point2D point = new Point(); // location of node
		ArrayList<Point2D> takenpositions = new ArrayList<Point2D>();

		// set random locations
		for (Node n : g.getVertices()) {
			x = random.nextInt(maxX + 1);
			y = random.nextInt(maxY + 1);

			if (x < 10)
				x = 10;
			if (y < 10)
				y = 10;

			point.setLocation(x, y);

			for (Point2D p : takenpositions) {
				if (p.getX() == point.getX() && p.getY() == point.getY()) {
					// position of node is already taken
					point.setLocation(x + 1, y + 1);
				}
			}

			Point2D tmp = new Point(x, y);
			takenpositions.add(tmp);

			vv.getGraphLayout().setLocation(n, point);
		}
		// end set random locations

		vv.repaint();
	}
}
